package com.anysoft.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.widget.Toast;

import com.anysoft.HostConstant;
import com.anysoft.MentorConst;
import com.anysoft.base.BaseActivity;
import com.anysoft.bean.UserBean;
import com.anysoft.fund.FundListAllActivity;
import com.anysoft.login.LoginActivity;
import com.anysoft.login.LoginByFingerPrintActivity;
import com.anysoft.login.LoginByGestureActivity;
import com.anysoft.login.SetGestureStep1Activity;
import com.anysoft.login.UserIconLoginActivity;
import com.anysoft.register.RegisterStep1_1Activity;
import com.anysoft.register.RegisterStep1_2Activity;
import com.anysoft.user.UserInfoEditActivity;
import com.anysoft.user.bankcard.AddBankCardActivity;
import com.anysoft.user.bankcard.EditMobileActivity;
import com.anysoft.utils.user.UserBeanUtil;
import com.anysoft.web.AssetsFundActivity;
import com.anysoft.web.FixInvestmentAllActivity;
import com.anysoft.web.RegistrationProcessActivity;
import com.anysoft.web.TransformQueryActivity;

/**
 * Created by wwq on 2017/2/6.
 */

public class ActivityUtils {

    public static void gotoLogin(Activity mActivity, boolean isHasUser,
                                 boolean isFromGestureActivity, String userId) {
        gotoLogin(mActivity, isHasUser, isFromGestureActivity, userId, false);
    }

    /**
     * @param mActivity
     * @param isHasUser             用户是否有登陆过的
     * @param isFromGestureActivity 是否是手势密码错误/忘记手势密码
     * @param userId                用户id
     */
    public static void gotoLogin(Activity mActivity, boolean isHasUser,
                                 boolean isFromGestureActivity, String userId, boolean isJudgeUserRecord) {
        Intent intent = new Intent();
        boolean is_finger_print_open = false;
        boolean is_gesture_open = false;
        //如果没有用户登录过,或者userId为空则去登录
        if (!isHasUser || StringUtils.isNullOrEmpty(userId)) {
            mActivity.startActivity(new Intent().setClass(mActivity, LoginActivity.class));
            return;
        }

        if (isJudgeUserRecord) {
            UserBean userBean = ((BaseActivity) mActivity).findUser(userId);
            if (userBean == null || !"1".equals(userBean.getRecordIdentNumber())) {
                mActivity.startActivity(new Intent().setClass(mActivity, LoginActivity.class));
                return;

            }
        } else {
            userId = UserBeanUtil.getInstance().getLastLoginAndRecordUserId();
            if (StringUtils.isNullOrEmpty(userId)) {
                mActivity.startActivity(new Intent().setClass(mActivity, LoginActivity.class));
                return;
            }
        }


        if (mActivity instanceof BaseActivity) {
            is_finger_print_open = ((BaseActivity) mActivity).getFingerPrintOpen(userId);
            is_gesture_open = ((BaseActivity) mActivity).getGestureOpen(userId);
        }
        if (is_finger_print_open) {
            intent.setClass(mActivity, LoginByFingerPrintActivity.class);
            intent.putExtra("userId", userId);
            mActivity.startActivity(intent);
            System.out.println("是否设置了指纹登录" + is_finger_print_open);
            return;
        }

//        System.out.println("userId--" + userId + ",sp--" + sp.getBoolean(userId + "CanGesture", true));
        if (is_gesture_open && PreferencesUtils.getBoolean(mActivity, userId + "CanGesture", true)) {
            intent.setClass(mActivity, LoginByGestureActivity.class);
            intent.putExtra("userId", userId);
            mActivity.startActivity(intent);
            return;
        }

        intent.setClass(mActivity, UserIconLoginActivity.class);
        intent.putExtra(MentorConst.IS_FROM_GESTURE, isFromGestureActivity);
        intent.putExtra("userId", TextUtils.isEmpty(userId) ? ((BaseActivity) mActivity).findUserId() : userId);
        mActivity.startActivity(intent);
    }

    /**
     * 判断最后一次登录的用户是否开启手势或指纹登录
     *
     * @return
     */
    public static boolean isOpenGestureOrFingerPrintLogin(Activity mActivity, String userId) {

        boolean is_finger_print_open = false;
        boolean is_gesture_open = false;
        if (StringUtils.isNullOrEmpty(userId)) {
            return false;
        }

        UserBean userBean = ((BaseActivity) mActivity).findUser(userId);
        if (userBean == null || !"1".equals(userBean.getRecordIdentNumber())) {
            return false;

        }

        if (mActivity instanceof BaseActivity) {
            is_finger_print_open = ((BaseActivity) mActivity).getFingerPrintOpen(userId);
            is_gesture_open = ((BaseActivity) mActivity).getGestureOpen(userId);
        }
        if (is_finger_print_open || is_gesture_open) {
            return true;
        }
        return false;
    }

    public static void gotoIconLoginActivity(Activity mActivity, boolean isFromGestureActivity, String userId) {
        Intent intent = new Intent();
        intent.setClass(mActivity, UserIconLoginActivity.class);
        intent.putExtra(MentorConst.IS_FROM_GESTURE, isFromGestureActivity);
        intent.putExtra("userId", userId);
        mActivity.startActivity(intent);
        mActivity.finish();
    }

    public static void gotoSetGestureActivity(Activity mActivity, String pwd) {
        Intent intent = new Intent();
        intent.setClass(mActivity, SetGestureStep1Activity.class);
        intent.putExtra(MentorConst.GET_PASSWORD, pwd);
        mActivity.startActivity(intent);
        mActivity.finish();
    }

    public static void gotoUserInfoMod(Activity mActivity, int requestCode) {
        Intent intent = new Intent(mActivity, UserInfoEditActivity.class);
        mActivity.startActivityForResult(intent, requestCode);
    }

    public static void gotoUserMobileMod(Activity mActivity, int requestCode) {
        Intent intent = new Intent(mActivity, EditMobileActivity.class);
        mActivity.startActivityForResult(intent, requestCode);
    }

    /**
     * 添加可定投的银行卡
     */
    public static void gotoAddPlanBankCard(Activity mActivity, int requestCode, String fundCode, String shareClass) {
        Intent intent = new Intent(mActivity, AddBankCardActivity.class);
        intent.putExtra("addType", "planBuy");
        intent.putExtra("planByFundNo", fundCode);
        intent.putExtra("shareClass", shareClass);
        mActivity.startActivityForResult(intent, requestCode);
    }

    /**
     * 跳转到全部基金页面
     */
    public static void gotoFundListActivity(Activity mActivity) {
        Intent intent = new Intent(mActivity, FundListAllActivity.class);
        mActivity.startActivity(intent);
    }

    /**
     * 跳转到定投页面
     */
    public static void gotoFixInvestmentAllActivity(Activity mActivity) {
        mActivity.startActivity(new Intent().setClass(mActivity, FixInvestmentAllActivity.class));

    }

    /**
     * 跳转全部资产
     */
    public static void gotoAssetsFundActivity(Activity mActivity) {
        mActivity.startActivity(new Intent().setClass(mActivity, AssetsFundActivity.class));

    }

    /**
     * 跳转到转换查询页面
     */
    public static void gotoTransformActivity(Activity mActivity) {
        mActivity.startActivity(new Intent().setClass(mActivity, TransformQueryActivity.class));

    }

    /**
     * 跳转到开户流程页面
     */
    public static void gotoRegistrationProcessActivity(Activity mActivity) {
        mActivity.startActivity(new Intent().setClass(mActivity, RegistrationProcessActivity.class));

    }

    /**
     * 跳转到开户页面
     * toIdCard 身份证填写页面
     * toBank 银行卡填写页面
     */
    public static void gotoRegisterStep1_2Activity(Activity mActivity, String name, String id_number, String address,String id_time, String toTarget) {
        Intent intent = new Intent(mActivity, RegisterStep1_2Activity.class);
        intent.putExtra("id_name", name);
        intent.putExtra("id_number", id_number);
        intent.putExtra("address", address);
        intent.putExtra("id_time", id_time);
        intent.putExtra("toTarget", toTarget);
        mActivity.startActivity(intent);

    }

    /**
     * 跳转到开户页面
     */
    public static void gotoRegisterStep1_1Activity(Activity mActivity) {
        mActivity.startActivity(new Intent().setClass(mActivity, RegisterStep1_1Activity.class));
    }

    /**
     * 跳转身份证上传页面
     * 开户 1  如已开户---告知客户端 进入登录界面
     * 大额交易 2
     * 忘记密码 3
     * 修改资料 4
     * 风险评测 5
     * 特殊身份 6
     */
    public static void gotoUploadIditityCardActivity(Activity mActivity, String identityType, String operationType) {
//        Intent intent = new Intent(mActivity, UpLoadIdentityCardActivity.class);
//        intent.putExtra("identityType", identityType);
//        intent.putExtra("operationType", operationType);
//        mActivity.startActivityForResult(intent, HostConstant.STRAT_UPLOAD_ID);

    }

    public static void openCameraActivity(Activity mActivity, int idcard_flag) {
//        Intent intent = new Intent(mActivity, CameraActivity.class);
//        intent.putExtra("nMainId", 2);
//        intent.putExtra("devcode", devcode);
//        intent.putExtra("idCardFlag", idcard_flag);
//        mActivity.startActivityForResult(intent, HostConstant.START_CAMETA);
    }

    public static void openLocalPick(Activity mActivity) {
        Intent intent = new Intent(
                Intent.ACTION_PICK,
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

        try {
            mActivity.startActivityForResult(Intent.createChooser(intent, "请选择一张图片"), HostConstant.START_CHOOSER);
        } catch (Exception e) {
            Toast.makeText(mActivity, "请安装文件管理器", Toast.LENGTH_SHORT).show();
        }
    }

    public static void openBankCardCamera(Activity mActivity) {
//        Intent intentTack = new Intent(mActivity, ScanCamera.class);
//        intentTack.putExtra("devcode", devcode);
//        mActivity.startActivityForResult(intentTack, HostConstant.START_BANK_CARD);
    }

    public static void startTargetActivity(Context context,Class clazz){
        context.startActivity(new Intent(context,clazz));
    }
}
